package com.iamteer.day22;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
主要回顾：转换流 

InputStreamReader
输入字节流的转换流

	
OutputStreamWriter
输出字节流的转换流：把输出字节流转换成输出字符流

转换流：
	1. 如果目前所获取到的是字节流，需要转换成字符流，则可使用转换流（字节流---->字符流）
	2. 转换流可以指定编码表进行遍文件读写
	
 */
public class Stream2Reader {

	public static void main(String[] args) throws IOException {
//		readTest();		//测试输入字节转换流
//		writeTtest();	//测试输出字节转换流
		writeTest2();	//指定编码输出
		readTest2();	//指定编码输入
	}

	public static void readTest() throws IOException {
		InputStream in = System.in;	//获取标准的输入流
		InputStreamReader inputStreamReader = new InputStreamReader(in);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			System.out.println(line);
		}
	}
	
	public static void writeTtest() throws IOException {
		 FileOutputStream out = new FileOutputStream("out.txt");
		 OutputStreamWriter oWriter = new OutputStreamWriter(out);
		 oWriter.write("大家好");
		 oWriter.close();
	}
	
	public static void writeTest2() throws IOException {
		FileOutputStream out = new FileOutputStream("writeTest2.txt");
		OutputStreamWriter writer = new OutputStreamWriter(out, "gbk");
		writer.write("热爱新中国！");
		writer.close();
	}
	
	public static void readTest2() throws IOException {
		FileInputStream in = new FileInputStream("writeTest2.txt");
		InputStreamReader reader = new InputStreamReader(in, "gbk");
		char[] cbuf = new char[1024];
		int length = -1;
		while ((length = reader.read(cbuf)) != -1) {
			System.out.println(new String(cbuf, 0, length));
		}
		reader.close();
	}
}
